"""
门户网站 - 套餐接口
"""
from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy import select

from app.dependencies import get_db
from app.schemas.common import ResponseModel
from app.models.content import ServicePackage, CustomPackage, PackageFeature, FAQ
from app.schemas.content import (
    ServicePackageResponse, CustomPackageResponse, PackageFeatureResponse, FAQResponse
)


router = APIRouter()


@router.get("/packages", response_model=ResponseModel)
async def get_packages_data(db: AsyncSession = Depends(get_db)):
    """
    获取套餐页面数据
    
    包含：
    - 所有启用的技术服务套餐
    - 所有启用的定制开发套餐
    - 所有启用的套餐功能对照
    - 所有启用的常见问题
    """
    # 获取所有启用的技术服务套餐
    service_package_result = await db.execute(
        select(ServicePackage)
        .where(ServicePackage.status == 1)
        .order_by(ServicePackage.sort_order.desc())
    )
    service_packages = service_package_result.scalars().all()
    
    # 获取所有启用的定制开发套餐
    custom_package_result = await db.execute(
        select(CustomPackage)
        .where(CustomPackage.status == 1)
        .order_by(CustomPackage.sort_order.desc())
    )
    custom_packages = custom_package_result.scalars().all()
    
    # 获取所有启用的套餐功能对照表
    package_feature_result = await db.execute(
        select(PackageFeature)
        .where(PackageFeature.status == 1)
        .order_by(PackageFeature.sort_order.asc())
    )
    package_features = package_feature_result.scalars().all()
    
    # 获取所有启用的FAQ
    faq_result = await db.execute(
        select(FAQ)
        .where(FAQ.status == 1)
        .order_by(FAQ.sort_order.asc())
    )
    faqs = faq_result.scalars().all()
    
    return ResponseModel(
        data={
            "service_packages": [ServicePackageResponse.model_validate(sp) for sp in service_packages],
            "custom_packages": [CustomPackageResponse.model_validate(cp) for cp in custom_packages],
            "package_features": [PackageFeatureResponse.model_validate(pf) for pf in package_features],
            "faqs": [FAQResponse.model_validate(faq) for faq in faqs]
        }
    )

